/**
 * 
 */
package br.com.mb;

import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import br.com.dao.GenericDAO;
import br.com.model.TbAjztCaoOprSeldCmcoOgmx;

@SessionScoped
@ManagedBean
public class DataTableRowEditing {

	private TbAjztCaoOprSeldCmcoOgmx ajztCaoOprSeldCmcoOgmx;
	private ArrayList<TbAjztCaoOprSeldCmcoOgmx> listaComunicacao;
	private ArrayList<SelectItem> listaTpComunic;		
	private int toEdit;
	
	public void submitItem(ActionEvent event) {
		// Edit use case if we already have an id
		if (ajztCaoOprSeldCmcoOgmx.getIdCmcoOgm() != -1) {
			TbAjztCaoOprSeldCmcoOgmx oldEdit = getTbAjztCaoOprSeldCmcoOgmxBuscaId(ajztCaoOprSeldCmcoOgmx
					.getIdCmcoOgm());
			// Only apply the changes if we can
			// There is a chance a user edits a car, then deletes it, then tries
			// to submit the edit
			// This logic will prevent that from causing an error
			if (oldEdit != null) {
//				oldEdit.applyValues(ajztCaoOprSeldCmcoOgmx);

				// Reset the values to blank
				ajztCaoOprSeldCmcoOgmx = new TbAjztCaoOprSeldCmcoOgmx();

				return;
			}
		}
		ajztCaoOprSeldCmcoOgmx.setIdCmcoOgm(listaComunicacao.size() + 1);
		// Add our new car
		listaComunicacao.add(ajztCaoOprSeldCmcoOgmx);
		// Reset the values to blank
		ajztCaoOprSeldCmcoOgmx = new TbAjztCaoOprSeldCmcoOgmx();
	}

	private TbAjztCaoOprSeldCmcoOgmx getTbAjztCaoOprSeldCmcoOgmxBuscaId(int id) {
		for (TbAjztCaoOprSeldCmcoOgmx currentItem : listaComunicacao) {
			if (id == currentItem.getIdCmcoOgm()) {
				return currentItem;
			}
		}
		return null;
	}
	
	public String editItem() {
		ajztCaoOprSeldCmcoOgmx = getTbAjztCaoOprSeldCmcoOgmxBuscaId(toEdit);

        return null;
    }

	public int getToEdit() {
		return toEdit;
	}
	
	public void setToEdit(int toEdit) { this.toEdit = toEdit; }

	public TbAjztCaoOprSeldCmcoOgmx getAjztCaoOprSeldCmcoOgmx() {
		return ajztCaoOprSeldCmcoOgmx;
	}

	public void setAjztCaoOprSeldCmcoOgmx(
			TbAjztCaoOprSeldCmcoOgmx ajztCaoOprSeldCmcoOgmx) {
		this.ajztCaoOprSeldCmcoOgmx = ajztCaoOprSeldCmcoOgmx;
	}

	public ArrayList<TbAjztCaoOprSeldCmcoOgmx> getListaComunicacao() {
		if (listaComunicacao == null) {
			listaComunicacao = (ArrayList<TbAjztCaoOprSeldCmcoOgmx>) new GenericDAO<TbAjztCaoOprSeldCmcoOgmx>(
					TbAjztCaoOprSeldCmcoOgmx.class).listaTodos();

		}
		return listaComunicacao;
	}

	public void setListaComunicacao(
			ArrayList<TbAjztCaoOprSeldCmcoOgmx> listaComunicacao) {
		this.listaComunicacao = listaComunicacao;
	}
	
	public ArrayList<SelectItem> getListaTpComunic() {
		return listaTpComunic;
	}

	public void setListaTpComunic(ArrayList<SelectItem> listaTpComunic) {
		this.listaTpComunic = listaTpComunic;
	}


}